package praca.gra;

import java.util.List;

import praca.framework.FPSCounter;
import praca.framework.MusicGL;
import praca.framework.SoundManagerGL;
import praca.framework.TextureGL;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Graphics;
import praca.interfejsy.Music;
import praca.interfejsy.Screen;
import praca.interfejsy.SoundManager;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;
import android.util.Log;

public class Ex05Menu extends Screen{
	
	Texture startGame;
	Texture help;
	Texture sounds;
	Texture troll;
	Graphics g;
	Troll trolls[];
	int NR_TROLLS = 10;
	TouchEvent ev;
	Vertices startVert = new Vertices(256, 64, true);
	Vertices helpVert = new Vertices(256, 64, true);
	Vertices soundVert = new Vertices(32, 32, true);
	Vertices trollVert = new Vertices(100, 100, true);
	SoundManager soundManager;
	int clickSound = -1;
	Music music;
	FPSCounter counter = new FPSCounter();
	
	public Ex05Menu(Game game){
		super(game);
		
		trolls = new Troll[NR_TROLLS];
		for(int i = 0; i < NR_TROLLS; i++) {
			trolls[i] = new Troll(100,100);
		}
		music = new MusicGL(game);
		music.loadMusic("brass.ogg");
		soundManager = new SoundManagerGL(game);
		/*
		 * Wczytywanie krotkiego dzwieku
		 */
		clickSound = soundManager.loadSound("click.ogg");
	}
	
	@Override
	public void update(float deltaTime) {
		
		g.drawColor(1, 1, 1);
		
		if(!game.getInput().getTouchEvents().isEmpty()){
			ev = game.getInput().getTouchEvents().remove(0);
			Log.d("touch", "Tacz");
			
			if(ev.getType() == TouchEvent.TOUCH_UP){
				if(isInsideStart(ev.getX(), ev.getY())){
					/*
					 * Jesli kliknieto przycisk start to wystarczy ustawic current screen
					 * i od razu rozpoczyna sie jego renderowanie
					 */
					soundManager.play(clickSound);//odtworzenie dzwieku
					game.setCurrentScreen(new Ex04Trolls(game));
					
				}
			}
			
			
		}
		
		g.setTransparency(0.3f);// funkcja ustawiajaca przezroczystosc dla wszystkiego co rysujemy nastepnie
		//ustawienie 1 to brak przezroczystosci
		
		for(int i = 0 ; i < NR_TROLLS; i++){
			trolls[i].update(deltaTime);
				g.clearTransformations();
				g.moveTexture(trolls[i].x, trolls[i].y);
				g.rotateTexture(trolls[i].rotation);
				g.drawTexture(troll, trollVert);
		}
		
		g.setTransparency(0.9f);//prawie nieprzezroczyste
		
		g.clearTransformations();
		g.moveTexture(game.getWidth()/2, game.getHeight()/1.5f);
		g.drawTexture(startGame, startVert);
		g.clearTransformations();
		g.moveTexture(game.getWidth()/2, game.getHeight()/2f);
		g.drawTexture(help, helpVert);
		
		g.clearTransformations();
		g.moveTexture(game.getWidth()/1.1f, game.getHeight()/6);
		g.drawTexture(sounds, soundVert);
		
	}
	
	private boolean isInsideStart(float u, float v){
		if(u < (game.getWidth()/2)+128 && u > (game.getWidth()/2)-128 
				&& v< game.getHeight()/1.5f+32 && v > game.getHeight()/1.5f-32)
			return true;
		else
			return false;
	}



	@Override
	public void pause() {
		music.pause();
		
	}

	@Override
	public void resume() {
		/*
		 * Ponowne ladowanie tekstur!!
		 */
		startGame = new TextureGL(game, "start.png");
		help = new TextureGL(game, "help.png");
		sounds = new TextureGL(game, "speaker.png");
		troll = new TextureGL(game, "troll.png");
		g = game.getGraphics();
		
		g.drawColor(1, 1, 1);
		g.setTextureTransparentBackground();
		g.clearTransformations();
		g.moveTexture(game.getWidth()/2, game.getHeight()/1.5f);
		g.drawTexture(startGame, startVert);
		g.clearTransformations();
		g.moveTexture(game.getWidth()/2, game.getHeight()/2f);
		g.drawTexture(help, helpVert);
		
		g.clearTransformations();
		g.moveTexture(game.getWidth()/1.1f, game.getHeight()/6);
		g.drawTexture(sounds, soundVert);
		music.play();
	}



	@Override
	public void dispose() {
		music.stop();
		
	}

}
